package hamaster.jpbc.pbs;

import java.util.Properties;

import it.unisa.dia.gas.jpbc.Element;
import hamaster.jpbc.pbc.PBC;
import hamaster.jpbc.pbc.PBCDriver;
import hamaster.jpbc.pbc.PublicParameter;

/**
 * 保存个人参数的类
 * @author <a href="mailto:wangyeee@gmail.com">Wang Ye</a>
 */
public class PersonalParameter extends PublicParameter {
	private final static String PG_POSTFIX = "pg";
	private final static String PG1_POSTFIX = "pg1";
	private final static String PH_POSTFIX = "ph";
	private final static String PRID_POSTFIX = "prid";
	private final static String PRIDX_POSTFIX = "pridx";

	/**
	 * 表示身份的参数rID 是rIDString到Element类型的映射
	 */
	protected Element rID;

	/**
	 * rID的字符串表示 如电子邮件地址
	 */
	private String rIDString;

	public PersonalParameter() {
	}

	public PersonalParameter(Properties properties, String prefix) {
		setFromProperties(properties, prefix);
	}

	public void setrID(Element rID) {
		this.rID = rID;
	}

	public Element getUser() {
		return rID;
	}

	public void setrIDString(String rIDString) {
		this.rIDString = rIDString;
	}

	public String getrIDString() {
		return rIDString;
	}

	@Override
	public Properties toProperties(String prefix) {
		Properties p = new Properties();
		String gk = PG_POSTFIX;
		String g1k = PG1_POSTFIX;
		String hk = PH_POSTFIX;
		String rk = PRID_POSTFIX;
		String rxk = PRIDX_POSTFIX;
		if (prefix != null && prefix.length() > 0) {
			gk = prefix + "." + PG_POSTFIX;
			g1k = prefix + "." + PG1_POSTFIX;
			hk = prefix + "." + PH_POSTFIX;
			rk = prefix + "." + PRID_POSTFIX;
			rxk = prefix + "." + PRIDX_POSTFIX;
		}
		p.setProperty(gk, PBCDriver.hex(g.toBytes()));
		p.setProperty(g1k, PBCDriver.hex(g1.toBytes()));
		p.setProperty(hk, PBCDriver.hex(h.toBytes()));
		p.setProperty(rk, PBCDriver.hex(rID.toBytes()));
		p.setProperty(rxk, PBCDriver.hex(rIDString.getBytes()));
		return p;
	}

	@Override
	public boolean setFromProperties(Properties properties, String prefix) {
		String gk = PG_POSTFIX;
		String g1k = PG1_POSTFIX;
		String hk = PH_POSTFIX;
		String rk = PRID_POSTFIX;
		String rxk = PRIDX_POSTFIX;
		if (prefix != null && prefix.length() > 0) {
			gk = prefix + "." + PG_POSTFIX;
			g1k = prefix + "." + PG1_POSTFIX;
			hk = prefix + "." + PH_POSTFIX;
			rk = prefix + "." + PRID_POSTFIX;
			rxk = prefix + "." + PRIDX_POSTFIX;
		}
		String gs = properties.getProperty(gk);
		String g1s = properties.getProperty(g1k);
		String hs = properties.getProperty(hk);
		String rs = properties.getProperty(rk);
		String rxs = properties.getProperty(rxk);
		if (gs == null || g1s == null || hs == null || rs == null || rxs == null)
			return false;
		PBC pbc = PBC.instance();
		g = pbc.getPairing().getG1().newElement();
		g1 = pbc.getPairing().getG1().newElement();
		h = pbc.getPairing().getG2().newElement();
		rID = pbc.getPairing().getZr().newElement();
		g.setFromBytes(PBCDriver.unhex(gs));
		g1.setFromBytes(PBCDriver.unhex(g1s));
		h.setFromBytes(PBCDriver.unhex(hs));
		rID.setFromBytes(PBCDriver.unhex(rs));
		rIDString = new String(PBCDriver.unhex(rxs));
		return true;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		PersonalParameter other = (PersonalParameter) obj;
		if (rID == null) {
			if (other.rID != null)
				return false;
		} else if (!rID.equals(other.rID))
			return false;
		if (rIDString == null) {
			if (other.rIDString != null)
				return false;
		} else if (!rIDString.equals(other.rIDString))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "[PersonalParameter]\nrID=" + rID + "\nrIDString=" + rIDString
				+ "\npg=" + g + "\npg1=" + g1 + "\nph=" + h + "\n";
	}

}
